(function($) {
	var defaults = {
		'text':'',
		'title':'ADCSoft',
		'index':-1,
		'queue':false,
		'sticky':false
	},
	msg_format = {
		'title':'ADCSoft',
		'text':'',
		'origin':_WEB_ROOT
	},
	gritter_options = {
		'title': 'ADCSoft',
		'text': '',
		'sticky': false,
		'time': 3000,
		'before_open': '',
		'before_close': '',
		'after_open': '',
		'after_close': '',
		'image': _WEB_ROOT + 'img/logo_mini.gif'
	}
	_options,
	index_actual = -1;
	
	function set_options(options){
		return _options = $.extend({}, defaults, options);
	}
	
	function get_message(index){
		if(!$.jStorage.index().length){
			return false;
		} else {
			msg = $.jStorage.get('_message_queue',-1);
			if (msg === -1){
				return false;
			}
			return msg[index];
		}
	}
	
	function count_messages(){
		return $.jStorage.index().length;
	}
	
	function mostrar_mensaje(index){
		if(index<0){
			//mostrar mensaje inmediato
			msg = $.extend({}, gritter_options, _options);
		} else {
			//mostrar mensaje del indice almacenado
			msg = get_message(index);
			if(!msg){
				msg = {'text':'el mensaje no existe o fue borrado'};
			}
			msg = $.extend({}, gritter_options, msg, {'after_close': function(){clear_message(index);}});
		}
		$.gritter.add(msg);
	}
	
	function clear_message(index){
		msgs = $.jStorage.get('_message_queue');
		tmp = [];
		for(var i=0,j=value.length; i<j; i++){
			tmp[i] = value[i];
		}
		msgs = tmp.slice(0,index).concat(tmp.slice(index +1));
		$.jStorage.flush();
		$.jStorage.set('_message_queue', msgs);
		index_actual = -1;
		if(!msgs.lenght){
			$('#message_queue').fadeOut('fast');
		}
	}
	
	function mostrar_todos(){
		msjs = $.jStorage.get('_message_queue', -1);
		if(msjs === -1){
			mostrar_mensaje(9999);
			return false;
		}
		gritter_options.sticky = true;
		for (var i=0, j=msjs.length; i<j; i++){
			mostrar_mensaje(i);
		}
		gritter_options.sticky = false;
	}
	
	function show_message_queue(){
		//verificando si ya existe el message_queue
		$mq = $('#message_queue');
		count = count_messages();
		if(!$mq.length && count){
			$mq = $('<div id="message_queue" class="ui-corner-bottom"><a href="#" title="'+ count +' mensajes"><image src="/adcsoft/img/icons/comment.png" alt="total: " />'+ count +'</a><br /><div><span id="un-queue-prev" class="ui-icon ui-icon-arrowthick-1-w" title="leer anterior"></span><span id="un-queue-all" class="ui-icon ui-icon-mail-closed" title="leer todos"></span><span id="un-queue-next" class="ui-icon ui-icon-arrowthick-1-e" title="leer siguiente"></span></div></div></div>')
					.hover(
						function(){
							$(this).stop().animate({height:'35px'}).children().show().end()
						},
						function(){
							$(this).children().hide().end().stop().animate({height:'4px'})
						}
					)
					.find('a').click(function(){
						if(index_actual == -1){
							index_actual = 0;
						}
						mostrar_mensaje(index_actual);
						}).end()
					.find('.un-queue-prev').click(function(){
						$.gritter.removeAll({
							after_close: function(){
								indx = index_actual - 1;
								if(indx>-1 && indx<count_messages()){
									index_actual = indx;
									mostrar_mensaje(indx);
								}
							}
						});
						}).end()
					.find('.un-queue-next').click(function(){
						$.gritter.removeAll({
							after_close: function(){
								indx = index_actual + 1;
								if(indx>-1 && indx<count_messages()){
									index_actual = indx;
									mostrar_mensaje(indx);
								}
							}
						});
						}).end()
					.find('.un-queue-all')
						.click(function(){
							mostrar_todos();
							})
						.end()
					.appendTo('#navigator');
		} else {
			//ya existe
			$('#message_queue').fadeIn('fast');
		}
		
	}
	
	function set_message(obj_message){
		if (typeof obj_message == 'string'){
			obj_message = $.extend({}, msg_format, {'text':'obj_message'});
		} else {
			obj_message = $.extend({}, msg_format, obj_message);
		}
		if(!obj_message.text || !obj_message.title){
			return false;
		}
		msjs = $.jStorage.get('_message_queue', []);
		msjs = $(msjs).add(obj_message);
		$.jStorage.set('_message_queue', msjs);
		index_actual = -1;
		show_message_queue();
	}
	
	function clear_all(){
		//mostrar_todos();
		$.jStorage.flush();
		index_actual = -1;
		$('#message_queue').fadeOut('fast');
	}
	
	$.alerta = function(action, options) {
		//verificando que los parámetros sean los correctos
		if(typeof action == 'object'){
			options = action;
			action = 'show';
		} else if((action!=='count' && action!=='queue' && action!=='clear' && action!=='clear_all' && action!=='show_all' && action!=='show') && typeof action == 'string') {
			options = {'text': action};
			action = 'show';
		}
		
		//estandarizando opciones
		_options = $.extend({}, defaults, options);
		switch (action){
			case 'queue':
				set_message(options);
				break;
			case 'count':
				return count_messages();
				break;
			case 'clear':
				clear_message(_options.index)
				break;
			case 'show_all':
				mostrar_todos();
				break;
			case 'clear_all':
				clear_all();
				break;
			case 'show':
				mostrar_mensaje(_options.index);
				break;
			default:
				show_message_queue();
		}
		
	};
})(jQuery);